package com.ytjj.qmyx.admin.service.imService.impl;

import com.ytjj.common.constants.im.ImRedisKeyConstant;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.mapper.ImServiceGroupMapper;
import com.ytjj.qmyx.admin.model.ImServiceGroupExample;
import com.ytjj.qmyx.admin.model.ImServiceUser;
import com.ytjj.qmyx.admin.model.ImSessionInfo;
import com.ytjj.qmyx.admin.model.dto.ImServiceGroupDto;
import com.ytjj.qmyx.admin.model.dto.ImServiceUserDto;
import com.ytjj.qmyx.admin.model.vo.ImServiceGroupVO;
import com.ytjj.qmyx.admin.model.vo.ImServiceUserVO;
import com.ytjj.qmyx.admin.service.imService.ImServiceGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ImServiceGroupServiceImpl implements ImServiceGroupService {

    @Resource
    private ImServiceGroupMapper imServiceGroupMapper;
    @Autowired
    private RedisService redisService;

    @Override
    public Integer insertGroup(ImServiceGroupVO imServiceGroupVO) {
        ImServiceUserDto imServiceUserDto = selectByName(imServiceGroupVO.getGroupName());
        if (null != imServiceUserDto){
            throw new ApiException("该组已存在");
        }
        imServiceGroupVO.setCreateTime(new Date());
        int count = imServiceGroupMapper.insertOne(imServiceGroupVO.getGroupName(),imServiceGroupVO.getCreateTime());
        return count;
    }

    @Override
    public Integer deleteGroup(ImServiceGroupVO imServiceGroupVO) {
        List<ImServiceGroupDto> list = imServiceGroupMapper.selectGroup(imServiceGroupVO.getId());
        if (!CollectionUtils.isEmpty(list)){
            List<ImServiceUserDto> serviceUsers = imServiceGroupMapper.selectByGroupId(imServiceGroupVO.getId());
            if (!CollectionUtils.isEmpty(serviceUsers)){
                throw new ApiException("该组有客服存在,不能删除");
            }
            int count = imServiceGroupMapper.updateById(imServiceGroupVO.getId());
            return count;
        }
        return 0;
    }

    @Override
    public Integer updateGroup(ImServiceGroupVO imServiceGroupVO) {
        ImServiceUserDto imServiceUserDto = selectByName(imServiceGroupVO.getGroupName());
        if (null != imServiceUserDto && imServiceUserDto.getId().equals(imServiceGroupVO.getId())) {
            throw new ApiException("该组已存在,不能修改为"+imServiceGroupVO.getGroupName());
        }
//        List<ImServiceGroupDto> list = imServiceGroupMapper.selectGroup(imServiceGroupVO.getId());
//        if (!CollectionUtils.isEmpty(list)){
            int count = imServiceGroupMapper.updateGroupName(imServiceGroupVO.getId(),imServiceGroupVO.getGroupName());
            return count;
//        }
    }

    @Override
    public List<ImServiceGroupDto> selectGroup(ImServiceGroupVO imServiceGroupVO) {
        List<ImServiceGroupDto> list = imServiceGroupMapper.selectGroup(imServiceGroupVO.getId());
        return list;
    }

    @Override
    public List<ImServiceUserDto> selectAllServiceUser() {
        List<ImServiceUserDto> list = imServiceGroupMapper.selectAllUser();
        return list;
    }


    /**
     * 客服分组中添加客服
     * 参数:groupId,userId,maxConNum
     */
    @Override
    public Integer addServiceUser(ImServiceUserVO imServiceUserVO) {
        int count = 0;
        ImServiceUserDto userDto = imServiceGroupMapper.selectByUserId(imServiceUserVO.getId());
        if (null != userDto && 0 == userDto.getGroupId()){
            count = imServiceGroupMapper.addServiceUser(imServiceUserVO.getId(),imServiceUserVO.getGroupId(),imServiceUserVO.getMaxConNum());
            try {
                if (count > 0) {
                    redisService.set(ImRedisKeyConstant.MAX_CONNECT_NUM + userDto.getUserId(), String.valueOf(imServiceUserVO.getMaxConNum()));
                }
            } catch (Exception e) {
                log.error("===更新客服最大接待上线到redis异常==={}, {}", userDto.getUserId(), imServiceUserVO.getMaxConNum());
            }
        }else {
            throw new ApiException("该客服已添加在 "+userDto.getGroupName()+" 中");
        }
        return count;
    }

    @Override
    public List<ImServiceUserDto> selectByGroupId(Integer groupId) {
        List<ImServiceUserDto> list = imServiceGroupMapper.selectByGroupId(groupId);
        return list;
    }

    @Override
    public Integer updateServiceUser(Integer id, Integer maxConNum) {
        Integer count = imServiceGroupMapper.updateServiceUser(id,maxConNum,null);
        try {
            if (count > 0) {
                ImServiceUserDto imServiceUser = imServiceGroupMapper.selectByUserId(id);
                redisService.set(ImRedisKeyConstant.MAX_CONNECT_NUM + imServiceUser.getUserId(), String.valueOf(maxConNum));
            }
        } catch (Exception e) {
            log.error("===更新客服最大接待上线到redis异常==={}, {}", id, maxConNum);
        }
        return count;
    }

    @Override
    public Integer removeServiceUser(Integer id) {
        Integer count = imServiceGroupMapper.updateServiceUser(id,null,0);
        return count;
    }

    public ImServiceUserDto selectByName(String name) {
        ImServiceUserDto imServiceUserDto = imServiceGroupMapper.selectByName(name);
        return imServiceUserDto;
    }
}
